package com.ux.iot.jetlinks.config;


import com.alibaba.fastjson.JSON;
import com.ux.iot.core.constant.IotContants;
import com.ux.iot.core.service.MessageHandlerManager;
import com.ux.iot.jetlinks.contant.JetlinksContants;
import com.ux.iot.jetlinks.pro.JetlinksProperties;
import com.ux.iot.jetlinks.runner.JetLinksWebosocketClientRunner;
import com.ux.iot.jetlinks.service.auth.JetlinkTokenManager;
import com.ux.iot.jetlinks.service.websocket.JetlinksConnectListener;
import com.ux.iot.jetlinks.service.websocket.SampleJetlinksWebosocketClient;
import com.ux.iot.jetlinks.service.websocket.SingleWebsocketClientEndpoint;
import com.ux.iot.jetlinks.service.websocket.handler.JetlinksMessageHandlerManager;
import com.ux.iot.jetlinks.service.websocket.handler.JetlinksMessageListener;
import com.ux.iot.jetlinks.service.websocket.handler.JetlinksSampleMessageListener;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.retry.annotation.EnableRetry;
import org.springframework.scheduling.annotation.EnableAsync;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;

/**
 * @author caixiaodao
 * @version 1.0
 * 功能描述
 * jetlink 消息发布订阅
 * @date 2021/11/15 17:17
 */
@ComponentScan("com.ux.iot.jetlinks")
@Slf4j
@EnableConfigurationProperties(JetlinksProperties.class)
@ConditionalOnProperty(prefix = JetlinksContants.PRO_PREFIX,name = IotContants.SWITCH,havingValue = "true")
@EnableRetry
@EnableAsync
public class JetlinksIotWebsocketAutoConfigure {
    @Autowired
    JetlinksProperties pro;
    @PostConstruct
    public void init(){
        log.info("开始加载jetlinks 配置{}", JSON.toJSONString(pro));
    }

    @ConditionalOnMissingBean(SampleJetlinksWebosocketClient.class)
    @Bean("jetlinksClient")

    public SampleJetlinksWebosocketClient sampleJetlinksWebosocketClient(
            JetlinkTokenManager jetlinkTokenManager,
            SingleWebsocketClientEndpoint endpoint){
        return  new SampleJetlinksWebosocketClient(pro.getWebsocket(),jetlinkTokenManager,endpoint);
    }
    @Bean("jetlinkTokenManager")
    @ConditionalOnMissingBean(JetlinkTokenManager.class)
    public JetlinkTokenManager  jetlinkTokenManager(){
        return  new JetlinkTokenManager(pro.getAuth());
    }

    @Bean("jetlinksEndpoint")
    @ConditionalOnMissingBean(SingleWebsocketClientEndpoint.class)
    public SingleWebsocketClientEndpoint singleWebsocketClientEndpoint( List<JetlinksConnectListener> listeners, JetlinksMessageListener  jetlinkMessageListener){
        if(listeners==null){
            listeners=new ArrayList<>();
        }
        SingleWebsocketClientEndpoint websocketClientEndpoint= new SingleWebsocketClientEndpoint();
        websocketClientEndpoint.setJetlinksMessageListener(jetlinkMessageListener);
        websocketClientEndpoint.setListeners(listeners);
        return websocketClientEndpoint;
    }

    @Bean("jetlinksMessageHandlerManager")
    @ConditionalOnMissingBean(MessageHandlerManager.class)
    public MessageHandlerManager messageHandlerManager(ApplicationContext applicationContext){
        return  new JetlinksMessageHandlerManager(applicationContext);
    }
    @Bean("jetlinkMessageListener")
    @ConditionalOnMissingBean(JetlinksMessageListener.class)
    public  JetlinksMessageListener getJetlinksMessageListener(MessageHandlerManager jetlinksMessageHandlerManager){
            return  new JetlinksSampleMessageListener(jetlinksMessageHandlerManager);
    }
    /**
     *  启动websocket 连接jetlinks 服务端
     * @param webosocketClient
     * @return
     */
    @Bean("jetLinksWebosocketClientRunner")
    public JetLinksWebosocketClientRunner jetLinksWebosocketClientRunner(SampleJetlinksWebosocketClient webosocketClient){
        return  new JetLinksWebosocketClientRunner(webosocketClient);
    }


}
